package com.baize.baizeapp.test.video;

import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ss.ttvideoengine.TTVideoEngine;
import com.ss.ttvideoengine.VideoEngineListener;
import com.ss.ttvideoengine.utils.Error;

public class HeiShiTTVideoViewJava extends FrameLayout {
    private final DisplayMode displayMode = new DisplayMode();
    private TextureView textureView;
    private TTVideoEngine ttVideoEngine;

    public HeiShiTTVideoViewJava(@NonNull Context context) { this(context,null); }


    public HeiShiTTVideoViewJava(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public HeiShiTTVideoViewJava(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.textureView = new TextureView(context);
        addView(textureView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        this.displayMode.setContainerView(this);
        this.displayMode.setDisplayView(this.textureView);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.displayMode.apply();
    }

    public void setDisplayMode(int displayMode) {
        this.displayMode.setDisplayMode(displayMode);
    }

    void initPlayer() {
        ttVideoEngine = new TTVideoEngine(getContext(), TTVideoEngine.PLAYER_TYPE_OWN);
        ttVideoEngine.setListener(new VideoEngineListener() {
            @Override
            public void onPlaybackStateChanged(TTVideoEngine ttVideoEngine, int i) {

            }

            @Override
            public void onLoadStateChanged(TTVideoEngine ttVideoEngine, int i) {

            }

            // ...省略其余实现方法
            @Override
            public void onVideoSizeChanged(TTVideoEngine engine, int width, int height) {
                displayMode.setVideoSize(width, height);
            }

            @Override
            public void onBufferingUpdate(TTVideoEngine ttVideoEngine, int i) {

            }

            @Override
            public void onPrepare(TTVideoEngine ttVifdeoEngine) {

            }

            @Override
            public void onPrepared(TTVideoEngine engine) {
                 displayMode.setVideoSize(engine.getVideoWidth(), engine.getVideoHeight());
            }

            @Override
            public void onRenderStart(TTVideoEngine ttVideoEngine) {

            }

            @Override
            public void onStreamChanged(TTVideoEngine ttVideoEngine, int i) {

            }

            @Override
            public void onCompletion(TTVideoEngine ttVideoEngine) {

            }

            @Override
            public void onError(Error error) {

            }

            @Override
            public void onVideoStatusException(int i) {

            }
        });
    }
}